package com.zut.Service.HttpJudgeImpl;

import cn.hutool.json.JSONUtil;
import com.zut.Pojo.Request.CodeEntity;
import com.zut.Pojo.Submissions;
import com.zut.Pojo.submissionsBody;
import com.zut.Util.HttpClientUtils;
import com.zut.dao.SubmissionsDao;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public abstract class getHttpResult {
    private final static String URL="http://192.168.88.140:2358/submissions/batch?base64_encoded=true&wait=false";
    @Autowired
    private SubmissionsDao submissionsDao;


    @NotNull
    public Boolean getResultBoolean(CodeEntity codeEntity, List<submissionsBody> list) {
        Map<String,List> sub=new HashMap<>();
        sub.put("submissions", list);
        System.err.println(JSONUtil.toJsonStr(sub));

        try {
            String tokens = HttpClientUtils.post(URL, JSONUtil.toJsonStr(sub));
            System.out.println(tokens);
            // 如果判断结果不为空
            if (tokens != null) {
                // 构建Submissions对象，更新判断结果
                Submissions update = Submissions.builder()
                        .id(codeEntity.getSubmissionID())
                        .judgmentResult(tokens)
                        .build();
                // 更新数据库中的判断结果
                submissionsDao.updateById(update);
                System.err.println(update);
                return true;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return true;
    }

}
